package com.bigegg.employee.service.impl;

import com.bigegg.employee.domain.*;
import com.bigegg.employee.mapper.TeacherClassMapper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.bigegg.employee.service.TypeUserService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
 * 辅导员端用户类型
 *
 * @author sun
 */
@Component
@Order(1)
public class TypeUser1Service implements TypeUserService {


    public static final String USER_ROLE = "辅导员";

    public static final int USER_TYPE = 1;

    @Autowired
    SysUserServiceImpl userService;


    @Autowired
    CRoleService cRoleService;

    @Autowired
    TeacherClassMapper teacherClassMapper;

    @Override
    public int getType() {
        return USER_TYPE;
    }

    @Override
    public String getTypeName() {
        return USER_ROLE;
    }

    @Override
    public boolean support(String role) {
        return USER_ROLE.equals(role);
    }

    @Override
    public void register(RegisterInfo registerInfo) {
        SysUser user = registerInfo.parse2User();
        SysRole role = cRoleService.getRoleByName(USER_ROLE);
        if (role == null) {
            throw new RuntimeException("无法查找到 [" + USER_ROLE + "] 的角色，确保系统初始化正确！");
        }
        user.setRoleIds(new Long[]{role.getRoleId()});
        validateUser(user);
        userService.insertUser(user);
        registerInfo.setId(user.getUserId());
    }

    private void validateUser(SysUser user) {
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) {
            throw new RuntimeException("注册用户'" + user.getUserName() + "'失败，登录账号已存在");
        } else if (user.getPhonenumber() != null && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            throw new RuntimeException("注册用户'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (user.getEmail() != null && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
            throw new RuntimeException("注册用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
    }


    /**
     * 查询辅导员端基本信息
     *
     * @param user 登录用户
     * @return 辅导员信息
     */
    @Override
    public TypeUserInfo getLoginUserInfo(SysUser user) {

        TypeTeacherInfo teacherInfo = new TypeTeacherInfo(user);

        // 查辅导员
        TeacherClass teacherClass = new TeacherClass();
        teacherClass.setTeacherId(user.getUserId());
        List<Classes> classesList = this.teacherClassMapper.findClassesOfTheTeacher(teacherClass);

        teacherInfo.setClasses(classesList);
        //TODO 通过辅导员service 查询辅导员信息 组合对象返回
        return teacherInfo;
    }

    @Override
    public TypeUserInfo getTypeUserInfo(Long userId) {
        return null;
    }
}
